package com.ge.up;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * @BelongsProject: SpringMVC
 * @BelongsPackage: com.ge.up
 * @Author: gejingyu
 * @Date: 2024/1/16
 * @Description: 文件上传
 */

@Controller
@RequestMapping("/up")
public class UpController {

    @RequestMapping("/test")
    @ResponseBody
    public String testUp(MultipartFile file, HttpSession session) throws IOException {
        //文件名
        String fileName = file.getOriginalFilename();
        //服务器上目标文件夹的实际路径
        String realPath = session.getServletContext().getRealPath("/文件夹路径");
        File targetFile = new File(realPath);
        //目标文件夹不存在时创建
        if (!targetFile.exists()){
            targetFile.mkdir();
        }
        //文件全路径
        String finalPath = realPath + File.separator + fileName;
        //文件数据写入
        file.transferTo(new File(finalPath));
        return "success";
    }
}
